This page last changed on Sep 21, 2007 by [email protected].

This section explains

How to Create a Simple GeoServer Plugin

for Geoserver 1.6.x using the following scenario:
Implementation of a "Hello World" service. The service should supply a capabilities document which advertises a single operation called "sayHello". The result of a sayHello operation is the simple string "Hello World".


Setup

The first step in creating our plug-in is setting up a maven project for it. The project will be called "hello".

  1. Create a new directory called hello anywhere on your file system.
  2. Add a maven pom called pom.xml to the hello directory:
    pom.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
    
      <!-- set parent pom to community pom -->
      <parent>
        <groupId>org.geoserver</groupId>
        <artifactId>community</artifactId>
        <version>1.6.0</version>
      </parent>
    
      <!-- declare depenency on geoserver main -->
      <dependencies>
        <dependency>
          <groupId>org.geoserver</groupId>
          <artifactId>main</artifactId>
          <version>1.6.0</version>
        </dependency>
      </dependencies>
    
      <groupId>org.geoserver</groupId>
      <artifactId>hello</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>Hello World Service Module</name>
    
      <repositories>
        <repository>
           <id>codehaus</id>
           <name>codehaus</name>
           <url>http://repository.codehaus.org/</url>
        </repository>
        <repository>
           <id>refractions</id>
           <name>refractions</name>
           <url>http://lists.refractions.net/m2</url>
        </repository>
    
      </repositories>
    
    </project>
    The parent of the above pom is org.geoserver.community. This makes our new plugin depend on all the GeoServer libraries.
  3. Create a java source directory, src/main/java under the hello directory
    hello/
      + pom.xml
      + src/
        + main/
          + java/

Creating the Plug-in

A plug-in is a collection of extensions realized as spring beans. Extensions and Extension Points are explained in greater detail here. In this example the extension point of interest is AbstractService.

  1. Create a class called HelloWorld which implements AbstractService:
    HelloWorld.java
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloWorld {
    
    	public HelloWorld() {
    		// Do nothing
    	}
    
    	public void sayHello(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException {
    		response.getOutputStream().write( "Hello World".getBytes() );
    	}
    }

    The service is relatively simple. It overrides the the doGet method writing out the string "Hello World".

  2. Create an applicationContext declaring the above class as a bean.
    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="helloService" class="HelloWorld">
    	</bean>
    
    	<bean id="helloService-1.0.0" class="org.geoserver.platform.Service">
    		<constructor-arg index="0" value="hello"/>
    		<constructor-arg index="1" ref="helloService"/>
    		<constructor-arg index="2" value="1.0.0"/>
    	</bean>
    </beans>

At this point the hello project should look like the following:

hello/
  + pom.xml
  + src/
    + main/
      + java/
        + HelloWorld.java
        + applicationContext.xml

The completed project can also be found here

Trying it Out

  1. Install the hello module:
    [hello]% mvn install
    [INFO] Scanning for projects...
    [INFO] ----------------------------------------------------------------------------
    [INFO] Building Hello World Service Module
    [INFO]    task-segment: [install]
    [INFO] ----------------------------------------------------------------------------
    [INFO] [resources:resources]
    [INFO] Using default encoding to copy filtered resources.
    [INFO] [compiler:compile]
    [INFO] Compiling 1 source file to /home/ak/geoserver/community/hello/target/classes
    [INFO] [resources:testResources]
    [INFO] Using default encoding to copy filtered resources.
    [INFO] [compiler:testCompile]
    [INFO] No sources to compile
    [INFO] [surefire:test]
    [INFO] No tests to run.
    [INFO] [jar:jar]
    [INFO] Building jar: /home/ak/geoserver/community/hello/target/hello-1.0.jar
    [INFO] [jar:test-jar {execution: default}]
    [WARNING] JAR will be empty - no content was marked for inclusion!
    [INFO] Building jar: /home/ak/geoserver/community/hello/target/hello-1.0-tests.jar
    [INFO] [install:install]
    [INFO] Installing /home/ak/geoserver/community/hello/target/hello-1.0.jar to /home/ak/.m2/repository/org/geoserver/hello/1.0/hello-1.0.jar
    [INFO] Installing /home/ak/geoserver/community/hello/target/hello-1.0-tests.jar to /home/ak/.m2/repository/org/geoserver/hello/1.0/hello-1.0-tests.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 6 seconds
    [INFO] Finished at: Fri Sep 21 14:52:31 EDT 2007
    [INFO] Final Memory: 27M/178M
    [INFO] -----------------------------------------------------------------------
  2. Copy target/hello-1.0.jar into the WEB-INF/lib directory of your GeoServer install
  3. Restart GeoServer
  4. Visit http://<host>/geoserver/ows?request=sayHello&service=hello&version=1.0.0

Alternative 1: Bundling with Web Module

An alternative is to declare a dependency from the web module on the new plugin project.

  1. Install the hello module as above.
  2. Edit web/pom.xml and add the following dependency:
    <dependency>
          <groupId>org.geoserver</groupId>
          <artifactId>hello</artifactId>
          <version>1.0</version>
        </dependency>
  3. Install and run the web module
    [web] mvn install jetty:run
  4. Visit http://localhost:8080/geoserver/ows?request=sayHello&service=hello&version=1.0.0

Alternative 2: Running from GeoServer Source

As an alternative to trying the plugin:

  1. Install the hello module
  2. Change directory to the web module
  3. Install the web module
  4. Copy <hello module>/target/hello-1.0.jar to <web module>/target/geoserver/WEB-INF/lib:
    [/dev/geoserver/web]% cp ~/hello/target/hello-1.0.jar target/geoserver/WEB-INF/lib
  5. Run the exploded war with Jetty:
    [/dev/geoserver/web]% mvn jetty6:run-exploded
  6. Visit http://localhost:8080/geoserver/ows?request=sayHello&service=hello&version=1.0.0

hello.png (image/png)
firefox_helloworld.png (image/png)
firefox_helloworld.png (image/png)
Document generated by Confluence on Jan 16, 2008 23:26